import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

class P2PLogin extends StatefulWidget {
  const P2PLogin({super.key});

  @override
  _P2PLoginState createState() => _P2PLoginState();
}

class _P2PLoginState extends State<P2PLogin> {
  var editCtrl = TextEditingController();
  String isFrontCamera = "true";

  @override
  void initState() {
    editCtrl.text = "192.168.3.4";
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('DDR Controller Client'),
      ),
      body: Center(
        child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text("连接服务器："),
              SizedBox(
                width: 260.0,
                child: TextFormField(
                    controller: editCtrl,
                    keyboardType: TextInputType.phone,
                    textAlign: TextAlign.center,
                    decoration: const InputDecoration(
                      contentPadding: EdgeInsets.all(10.0),
                      hintText: '请输入服务器的IP',
                    )),
              ),
              // SizedBox(
              //     width: 260.0,
              //     height: 48.0,
              //     child: Row(
              //         mainAxisAlignment: MainAxisAlignment.center,
              //         children: [
              //           Checkbox(
              //               value: isFrontCamera=="true"?true:false,
              //               onChanged: (v) {
              //                 isFrontCamera = v.toString();
              //                 setState(() {});
              //               }),
              //           const Text("前置摄像头")
              //         ])),
              SizedBox(
                width: 260.0,
                height: 48.0,
                child: MaterialButton(
                  color: Colors.grey,
                  child: const Text(
                    '连接',
                  ),
                  onPressed: () => handleJoin(editCtrl.text),
                ),
              ),
              const SizedBox(height: 24),
              // Text("前置摄像头比较耗电", style: TextStyle(color: Colors.grey))
            ]),
      ),
    );
  }

  handleJoin(ip) {
    var map = {'ip': ip, "isFrontCamera": isFrontCamera};
    context.push(Uri(path: '/controller', queryParameters: map).toString());
  }
}
